package com.junjun.ssm.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Autowired
    CachingConnectionFactory connectionFactory;

    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean//队列
    public Queue queueOne() {
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadDirectExchange");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","dead.key");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.nonDurable("queueOne").withArguments(args).build();
    }

    @Bean//队列
    public Queue queueTwo() {
        return new Queue("queueTwo");
    }

    @Bean//队列
    public Queue queueThree() {
        return new Queue("queueThree");
    }

    @Bean//队列
    public Queue queueFour() {
        return new Queue("queueFour");
    }

    @Bean//死信队列
    public Queue deadQueue(){
        return new Queue("deadQueue");
    }

    @Bean//默认交换机
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }


    @Bean//主题交换机
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean//扇形交换机
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

     @Bean //死信交换机  就是默认交换机
     public DirectExchange deadDirectExchange(){
        return new DirectExchange("deadDirectExchange");
     }


    //绑定交换机和队列
    @Bean//绑定默认交换机和队列1
    public Binding queueOneToDirectExchange(Queue queueOne, DirectExchange directExchange) {
        return BindingBuilder.bind(queueOne).to(directExchange).with("movie.order");
    }


    @Bean//绑定主题交换机和队列2
    public Binding queueOneToTopicExchange(Queue queueTwo, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTwo).to(topicExchange).with("movie.#");
    }
    @Bean//绑定扇形交换机和队列3
    public Binding queueOneToFanoutExchange(Queue queueThree, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queueThree).to(fanoutExchange);
    }

    @Bean//绑定扇形交换机和队列4
    public Binding queueOneToFanoutExchangeToFour(Queue queueFour, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queueFour).to(fanoutExchange);
    }


    @Bean//绑定死信交换机
    public Binding deadQueueToDeadDirectExchange(Queue deadQueue,DirectExchange deadDirectExchange){
        return BindingBuilder.bind(deadQueue).to(deadDirectExchange).with("dead.key");
    }
}
